package com.amir.common.mvp.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.amir.common.R;
import com.amir.common.holder.CommonViewHolder;
import com.amir.common.utils.MyProgressLoading;
import com.amir.common.mvp.view.IView;
import com.amir.common.utils.ToastUtil;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


@Deprecated
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> implements IView {

    protected LayoutInflater layoutInflater;
    private CommonViewHolder mCommonViewHolder;
    protected List<T> dataList;
    protected static final int VIEW_TYPE_ITEM = 1;
    protected static final int VIEW_TYPE_EMPTY = 0;
    protected int layoutId;
    protected Context mContext;
    private MyProgressLoading mLoading;
    public BaseRecyclerAdapter(Context context, List<T> dataList, int layoutId) {
        this.layoutInflater = LayoutInflater.from(context);
        this.dataList = dataList;
        this.layoutId = layoutId;
        this.mContext = context;
        mLoading = new MyProgressLoading(mContext, R.style.DialogStyle);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(layoutId, parent, false);
        return new CommonViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(CommonViewHolder holder, int position) {
        if (dataList.size() != 0) {
            setHolder(holder);

            bindData(holder, dataList.get(position), position);
//            try {
//            } catch (Exception e) {
//                LogUtils.Companion.E(e.getMessage());
//            }

        }

    }

    @Nullable
    @Override
    public Context getViewContext() {
        return mContext;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    protected abstract void bindData(CommonViewHolder holder, T data, int position);


    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {

            ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (dataList.size() == 0) {


                        return ((GridLayoutManager) layoutManager).getSpanCount();
                    }

                    return 1;
                }
            });

        }

//        else if (layoutManager instanceof StaggeredGridLayoutManager) {
//            if (dataList.size() == 0) {
//
//
//                ((StaggeredGridLayoutManager) layoutManager).setSpanCount(1);
//            } else {
//                ((StaggeredGridLayoutManager) layoutManager).setSpanCount(((StaggeredGridLayoutManager) layoutManager).getSpanCount());
//            }
//        }


    }

    public void addData(List<T> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public List<T> getData() {

        return dataList;
    }

    public void setHolder(CommonViewHolder holder) {
        this.mCommonViewHolder = holder;
    }

    public CommonViewHolder getHolder() {

        return mCommonViewHolder;
    }

    public void clearData() {
        this.dataList.clear();
    }

    public void removeData(int postion) {
        this.dataList.remove(postion);
        this.notifyDataSetChanged();
    }

    protected OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItem(int pos);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @Override
    public void showLoading() {
        mLoading.show();

    }

    @Override
    public void dismissLoading() {
        mLoading.dismiss();
    }


    @Override
    public void showDialog(@Nullable String mess, int code) {

    }



    @Override
    public void onError(String string) {
        Message message = new Message();
        message.obj = string;
        message.what = 0;
        mHandler.sendMessage(message);
        String[] array = {"", ""};
        List<String> list = new ArrayList<String>(array.length);
    }

    @Override
    public void onAgainLogin() {
        mHandler.sendEmptyMessage(1);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {

                ToastUtil.Companion.show((String) msg.obj);
            } else {
//                SPUtils.Companion.remove(Contents.Token);
//                AppManager.getAppManager().finishAllActivity();
//                startActivity(new Intent(mContext, LoginActivity.class));
            }
        }
    };

}
